package l2ft.gameserver.model.instances;

import l2ft.gameserver.ai.CtrlIntention;
import l2ft.gameserver.ai.MainFakePc;
import l2ft.gameserver.model.Creature;
import l2ft.gameserver.model.Player;
import l2ft.gameserver.model.Skill;
import l2ft.gameserver.model.entity.events.fightclubmanager.FightClubTeam;
import l2ft.gameserver.model.items.FakePcInventory;
import l2ft.gameserver.model.items.ItemInstance;
import l2ft.gameserver.network.l2.s2c.ActionFail;
import l2ft.gameserver.network.l2.s2c.MagicSkillUse;
import l2ft.gameserver.network.l2.s2c.MyTargetSelected;
import l2ft.gameserver.network.l2.s2c.NpcInfo;
import l2ft.gameserver.network.l2.s2c.StatusUpdate;
import l2ft.gameserver.network.l2.s2c.ValidateLocation;
import l2ft.gameserver.templates.StatsSet;
import l2ft.gameserver.templates.item.WeaponTemplate;
import l2ft.gameserver.templates.npc.NpcTemplate;
import l2ft.gameserver.utils.ItemFunctions;

public class FakePcInstance extends NpcInstance
{
	public int race;
	public int sex;
	public int clazz;
	public String title;
	public String name;
	public int hairStyle;
	public int hairColor;
	public int face;
	public byte team = 0;
	public int nameColor = 0xFFFFFF;
	public byte hero;
	public int pdUnder;
	public int pdHead;
	public int pdRHand;
	public int pdLHand;
	public int pdGloves;
	public int pdChest;
	public int pdLegs;
	public int pdFeet;
	public int pdBack;
	public int lRing;
	public int rRing;
	public int lEarring;
	public int rEarring;
	public int necklace;
	public int pdLRHand;
	public int pvpFlag = 0;
	public byte invisible;
	private static final int[] _soulShotSkillIds = { 2061, 2155, 2156, 2157, 2158, 2159 };
	private static final int[] _spiritShotSkillIds = { 2061, 2160, 2161, 2162, 2163, 2164 };
	private final FakePcInventory _inventory = new FakePcInventory(this);
	
	public FakePcInstance(int objectId, NpcTemplate template) 
	{
		super(objectId, template);
		setSet(template.statsSet);
		addItems();
	}
	
	@Override
	public MainFakePc getAI()
	{
		return (MainFakePc) super.getAI();
	}
	
	private void addItems()
	{
		int[] items = {pdUnder, pdHead, pdRHand, pdLHand, pdGloves, pdChest, pdLegs, pdFeet, lRing, rRing, lEarring, rEarring, necklace};
		for(int item : items){
			if(item != 0)
			{
				ItemInstance createditem = ItemFunctions.createItem(item, 1);
				_inventory.addItem(createditem);
			}
		}
	}
	
	@Override
	public void doAttack(Creature target) 
	{
		int weaponGrade = getInventory().getItemInSlot(FakePcInventory.PAPERDOLL_RHAND).getCrystalType().externalOrdinal;
		broadcastPacket(new MagicSkillUse(this, this, _soulShotSkillIds[weaponGrade], 1, 0, 0));
		if(target.isNpc() && ((NpcInstance)target).isFakePc()){
			if(pvpFlag == 0){
				//NpcInfo packet = new NpcInfo(this, null);
				//broadcastPacketToOthers(packet);
				//pvpFlag = 1; TODO
				//((MainFakePc)target.getAI()).setTarget(this);
			}
		}
		super.doAttack(target);
	}
	
	@Override
	public void doCast(Skill skill, Creature target, boolean forceUse) 
	{
		if(skill.getId() == 1557)//Servitor share
			return;
		if(!target.equals(this) && target.isNpc() && ((NpcInstance)target).isFakePc()){
			if(pvpFlag == 0 && (skill.isOffensive() || target instanceof FakePcInstance && ((FakePcInstance)target).pvpFlag == 1)){
				//NpcInfo packet = new NpcInfo(this, null);
				//broadcastPacketToOthers(packet);
				//pvpFlag = 1; TODO
				//((MainFakePc)target.getAI()).setTarget(this);
			}
		}
		setCurrentMp(getMaxMp());
		int weaponGrade = getInventory().getItemInSlot(FakePcInventory.PAPERDOLL_RHAND).getCrystalType().externalOrdinal;
		broadcastPacket(new MagicSkillUse(this, this, _spiritShotSkillIds[weaponGrade], 1, 0, 0));
		super.doCast(skill, target, forceUse);
	}
	
	public void setTeam(int index)
	{
		nameColor = FightClubTeam.TEAM_NAMES.values()[index]._nameColor;
		broadcastPacketToOthers(new NpcInfo(this, null));
	}
	
	private void setSet(StatsSet set)
	{
		name = set.getString("name");
		title = set.getString("title");
		race = set.getInteger("race");
		sex = set.getInteger("sex");
		clazz = set.getInteger("class");
		hairStyle = set.getInteger("hair_style");
		hairColor = set.getInteger("hair_color");
		face = set.getInteger("face");
		hero = (byte) set.getInteger("hero");
		pdUnder = set.getInteger("pd_under");
		pdHead = set.getInteger("pd_head");
		pdRHand = set.getInteger("pd_rhand");
		pdLHand = set.getInteger("pd_lhand");
		pdGloves = set.getInteger("pd_gloves");
		pdChest = set.getInteger("pd_chest");
		pdLegs = set.getInteger("pd_legs");
		pdFeet = set.getInteger("pd_feet");
		pdBack = set.getInteger("pd_back");
		lRing = set.getInteger("lRing");
		rRing = set.getInteger("rRing");
		lEarring = set.getInteger("lEarring");
		rEarring = set.getInteger("rEarring");
		necklace = set.getInteger("necklace");
		pvpFlag = set.getInteger("pvp_flag");
		invisible = (byte) set.getInteger("invisible");
	}
	
	public FakePcInventory getInventory()
	{
		return _inventory;
	}
	
	@Override
	protected void onDeath(Creature killer, Skill skill)
	{
		getAI().iDied(this);
		super.onDeath(killer, skill);
	}
	
	@Override
	public boolean isInvul()
	{
		return false;
	}
	
	@Override
	public WeaponTemplate getActiveWeaponItem()
	{
		final ItemInstance weapon = getInventory().getItemInSlot(FakePcInventory.PAPERDOLL_RHAND);

		if(weapon == null)
			return null;

		return (WeaponTemplate) weapon.getTemplate();
	}
	
	@Override
	public void onAction(Player player, boolean shift)
	{
		if(!isTargetable())
		{
			player.sendActionFailed();
			return;
		}

		if(player.getTarget() != this)
		{
			player.setTarget(this);
			if(player.getTarget() == this)
				player.sendPacket(new MyTargetSelected(getObjectId(), player.getLevel() - getLevel()), makeStatusUpdate(StatusUpdate.CUR_HP, StatusUpdate.MAX_HP));

			player.sendPacket(new ValidateLocation(this), ActionFail.STATIC);
			return;
		}

		if(isAutoAttackable(player))
		{
			player.getAI().Attack(this, false, shift);
			return;
		}

		if(!isInRange(player, INTERACTION_DISTANCE))
		{
			if(player.getAI().getIntention() != CtrlIntention.AI_INTENTION_INTERACT)
				player.getAI().setIntention(CtrlIntention.AI_INTENTION_INTERACT, this, null);
			return;
		}

		player.sendActionFailed();
		player.stopMove(false);
	}
	
	@Override
	public boolean getChargedSoulShot()
	{
		return true;
	}
	
	@Override
	public int getChargedSpiritShot()
	{
		return ItemInstance.CHARGED_BLESSED_SPIRITSHOT;
	}
	
	@Override
	public boolean isFakePc()
	{
		return true;
	}
}